package com.hwadee.JPKC.ceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.hwadee.JPKC.entity.user;

/**
 * 

* @ClassName: UserRoleInterceptor

* @Description: TODO(用户权限)

* @author hui

* @date 2020-03-16

* @Copyright ? 四川华迪信息有限公司
 */
public class UserRoleInterceptor implements HandlerInterceptor{

	/**
     * Controller业务方法执行之前执行
     * @return 返回值决定请求是放行还是拦截
     * false:表示拦截
     * true:表示放行
     */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		
		user u = (user) request.getSession().getAttribute("u");
		if(null == u) {
			response.sendRedirect("/admin/User/teacherLogin");
            return false;
		}else if(u.getUserRole().equals("老师")) {
			return true;
		}else {
			response.sendRedirect("/index/");
			return false;
		}
		
	}

	 /**
     * 此方法在Controller的业务方法执行之后执行
     */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		System.err.println("此方法在Controller的业务方法执行之后执行");
		
	}

	/**
     * 此方法在Controller的业务方法执行结束并且视图解析完成后执行
     */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		System.err.println("此方法在Controller的业务方法执行结束并且视图解析完成后执行");
	}

}
